<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Variables</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.basic.syntax.html" title="Chapter 3. Basic Syntax">
<link rel="prev" href="language.basic.syntax.html" title="Chapter 3. Basic Syntax">
<link rel="next" href="language.syntax.functions.html" title="Functions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Variables</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.basic.syntax.html">Prev</a> </td>
<th width="60%" align="center">Chapter 3. Basic Syntax</th>
<td width="20%" align="right"> <a accesskey="n" href="language.syntax.functions.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Variables">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.syntax.variables"></a>Variables</h2></div></div></div>
<p>
  Template variables start with the $dollar sign. They can contain numbers,
  letters and underscores, much like a
  <a class="ulink" href="http://php.net/language.variables" target="_top">PHP variable</a>.
  You can reference arrays
  by index numerically or non-numerically. Also reference
  object properties and methods.</p>
<p>
  <a class="link" href="language.config.variables.html" title="Variables loaded from config files">Config file variables</a>
  are an exception to the $dollar syntax
  and are instead referenced with surrounding #hashmarks#, or
  via the
  <a class="link" href="language.variables.smarty.html#language.variables.smarty.config" title="{$smarty.config}">
  <em class="parameter"><code>$smarty.config</code></em></a> variable.
 </p>
<div class="example">
<a name="id343899"></a><p class="title"><b>Example 3.2. Variables</b></p>
<div class="example-contents"><pre class="programlisting">

{$foo}        &lt;-- displaying a simple variable (non array/object)
{$foo[4]}     &lt;-- display the 5th element of a zero-indexed array
{$foo.bar}    &lt;-- display the "bar" key value of an array, similar to PHP $foo['bar']
{$foo.$bar}   &lt;-- display variable key value of an array, similar to PHP $foo[$bar]
{$foo-&gt;bar}   &lt;-- display the object property "bar"
{$foo-&gt;bar()} &lt;-- display the return value of object method "bar"
{#foo#}       &lt;-- display the config file variable "foo"
{$smarty.config.foo} &lt;-- synonym for {#foo#}
{$foo[bar]}   &lt;-- syntax only valid in a section loop, see {section}
{assign var=foo value='baa'}{$foo} &lt;--  displays "baa", see {assign}

Many other combinations are allowed

{$foo.bar.baz}
{$foo.$bar.$baz}
{$foo[4].baz}
{$foo[4].$baz}
{$foo.bar.baz[4]}
{$foo-&gt;bar($baz,2,$bar)} &lt;-- passing parameters
{"foo"}       &lt;-- static values are allowed

{* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*}
{$smarty.server.SERVER_NAME}

Math and embedding tags:

{$x+$y}                             // will output the sum of x and y.
{assign var=foo value=$x+$y}        // in attributes 
{$foo[$x+3]}                        // as array index
{$foo={counter}+3}                  // tags within tags
{$foo="this is message {counter}"}  // tags within double quoted strings

Defining Arrays:

{assign var=foo value=[1,2,3]}
{assign var=foo value=['y'=&gt;'yellow','b'=&gt;'blue']}
{assign var=foo value=[1,[9,8],3]}   // can be nested

Short variable assignment:

{$foo=$bar+2}
{$foo = strlen($bar)}               // function in assignment
{$foo = myfunct( ($x+$y)*3 )}       // as function parameter 
{$foo.bar=1}                        // assign to specific array element
{$foo.bar.baz=1}                    
{$foo[]=1}                          // appending to an array

Smarty "dot" syntax (note: embedded {} are used to address ambiguities):

{$foo.a.b.c}        =&gt;  $foo['a']['b']['c'] 
{$foo.a.$b.c}       =&gt;  $foo['a'][$b]['c']         // with variable index
{$foo.a.{$b+4}.c}   =&gt;  $foo['a'][$b+4]['c']       // with expression as index
{$foo.a.{$b.c}}     =&gt;  $foo['a'][$b['c']]         // with nested index

PHP-like syntax, alternative to "dot" syntax:

{$foo[1]}             // normal access
{$foo['bar']}
{$foo['bar'][1]}
{$foo[$x+$x]}         // index may contain any expression
{$foo[$bar[1]]}       // nested index
{$foo[section_name]}  // smarty {section} access, not array access!

Variable variables:

$foo                     // normal variable
$foo_{$bar}              // variable name containing other variable 
$foo_{$x+$y}             // variable name containing expressions 
$foo_{$bar}_buh_{$blar}  // variable name with multiple segments
{$foo_{$x}}              // will output the variable $foo_1 if $x has a value of 1.

Object chaining:

{$object-&gt;method1($x)-&gt;method2($y)}

Direct PHP function access:

{time()}


  </pre></div>
</div>
<br class="example-break"><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    Although Smarty can handle some very complex expressions and syntax, it is a good rule of
    thumb to keep the template syntax minimal and focused on presentation. If you find your
    template syntax getting too complex, it may be a good idea to move the bits that do not
    deal explicitly with presentation to PHP by way of plugins or modifiers.
 </p>
</div>
<p>Request variables such as <code class="literal">$_GET</code>,
 <code class="literal">$_SESSION</code>, etc are available via the
 reserved <a class="link" href="language.variables.smarty.html" title="{$smarty} reserved variable">
 <em class="parameter"><code>$smarty</code></em></a> variable.
 </p>
<p>
  See also <a class="link" href="language.variables.smarty.html" title="{$smarty} reserved variable">
  <em class="parameter"><code>$smarty</code></em></a>,
  <a class="link" href="language.config.variables.html" title="Variables loaded from config files">config variables</a>
  <a class="link" href="language.function.assign.html" title="{assign}"><code class="varname">{assign}</code></a>
  and
  <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a>.
 </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.basic.syntax.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.basic.syntax.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.syntax.functions.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 3. Basic Syntax </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Functions</td>
</tr>
</table>
</div>
</body>
</html>
